package ltd.hyangyang.rpc.core.remoting.server;

import lombok.SneakyThrows;
import ltd.hyangyang.rpc.common.bean.Request;
import ltd.hyangyang.rpc.common.exception.RpcException;

import java.lang.reflect.Method;

public class RemotingHandler {

    @SneakyThrows
    public Object invokeFunction(Request request){
        ClassLoader classLoader = this.getClass().getClassLoader();
        Class<?> clazz = classLoader.loadClass(request.getCanonicalName());
        Class<?> returnType = request.getReturnType();
        Method method = clazz.getMethod(request.getMethodName(), request.getParameterTypes());
        //TODO 限制返回值
        Object result = method.invoke(clazz.newInstance(), request.getParameters());

        if (result.getClass().equals(returnType)){
            return result;
        }
       throw new RpcException("返回值是一个"+clazz+"类型"+"而你的实现类是"+result.getClass()+"类型");
    }
}
